import { HiddenRowColPlugin } from '../../../pugins/hidden-row-col.plugin';
import { OperateBase } from '../../common/operate-base';

export class HiddenCol extends OperateBase {
    protected container: HTMLElement;
    protected plugin: HiddenRowColPlugin
    constructor() {
        super();
    }
    public getDom() {
        this.container = document.createElement('div');
        this.container.setAttribute('class', this.containerClassName);
        this.container.innerHTML=`<span class="z-tools-icon icon iconfont icon-hiddencloumn"></span>`;
        return this.container;
    }
    public selectChange(select: any, cells: any[]): void {
        
    }
    public afterDomInit(): void {
        this.plugin = this.zTableInstance.pluginManager.getPlugin(HiddenRowColPlugin.pluginName);
        this.zTableInstance.getHook().afterSelect.add(() => {
            this.toggleDisabled();
        });
        this.toggleDisabled();
        let iconEle = this.container.querySelector('.icon-hiddencloumn') as HTMLElement;
        iconEle.addEventListener('click', () => {
            if(iconEle.classList.contains('z-table-icon-disabled')) {
                return;
            }
            let select = this.plugin.getSelectInfo();
            const hiddenCols = this.plugin.getHiddenCols();
            let hasVisualCol = false;
            for(let col = select.col; col < select.col + select.colspan; col++) {
                if(hiddenCols.indexOf(col) === -1) {
                    hasVisualCol = true;
                    break;
                }
            }
            console.log(hasVisualCol)
            if(hasVisualCol) {
                this.plugin.hiddenCols();
            } else {
                this.plugin.cancelHiddenCols();
            }
        });
    }
    protected toggleDisabled() {
        let select = this.plugin.getSelectInfo();
        let iconEle = this.container.querySelector('.icon-hiddencloumn') as HTMLElement;
        if(select) {
            iconEle.classList.remove('z-table-icon-disabled');
        } else {
            iconEle.classList.add('z-table-icon-disabled');
        }
    }
}